package com.bdqn;

import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

/**
 * lzl
 */
public class PersonReflection_5 {
    public static void main(String[] args) {
        Method [] methods = Person.class.getDeclaredMethods();

        System.out.println("方法展示");
        for (Method method : methods){
            System.out.println("方法名："+method.getName());
            System.out.println("返回值类型："+method.getReturnType().getName());

            Class [] params = method.getParameterTypes();
            if(params.length == 0){
                System.out.println("该构造方法没有参数");
            }else{
                System.out.print("该构造方法的参数列表为:[");
                for (int i = 0; i < params.length; i++) {
                    if(i!=0){
                        System.out.print(",");
                    }
                    System.out.print(params[i].getName());
                }
                System.out.println("]");
            }

            System.out.print("访问修饰符:");
            int modifier = method.getModifiers();
            //判断该属性的访问修饰符
            if((modifier & Modifier.PUBLIC) == Modifier.PUBLIC)
                System.out.println("public");
            else if((modifier & Modifier.PROTECTED) == Modifier.PROTECTED)
                System.out.println("PROTECTED");
            else if((modifier & Modifier.PRIVATE) == Modifier.PRIVATE)
                System.out.println("PRIVATE");
            else
                System.out.println("default(package)");

            //判断该属性书否有static修饰符
            if((modifier & Modifier.STATIC) == Modifier.STATIC)
                System.out.println("这是一个静态属性");

            //final
            if((modifier & Modifier.FINAL) == Modifier.FINAL)
                System.out.println("这是一个final属性");

            //abstract
            if((modifier & Modifier.ABSTRACT) == Modifier.ABSTRACT)
                System.out.println("这是一个abstract属性");

            //synchronized
            if((modifier & Modifier.SYNCHRONIZED) == Modifier.SYNCHRONIZED)
                System.out.println("这是一个synchronized属性");

            //获取方法所属的类或接口的Class实例
            Class declaringClass = method.getDeclaringClass();
            System.out.println("方法声明在："+declaringClass.getName()+"中");

            Class [] exceptions = method.getExceptionTypes();
            if(exceptions.length > 0){
                System.out.print("该方法抛出的异常有:[");
                for (int i = 0; i < exceptions.length; i++) {
                    if(i!=0){
                        System.out.print(",");
                    }
                    System.out.print(exceptions[i].getName());
                }
                System.out.println("]");
            }
            System.out.println("-----------------------------------------------------");
        }
    }
}
